﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace clock
{


        public partial class Form1 : Form
        {
            private Timer timer;
            private int hour;
            private int minute;
            private int second;

            public Form1()
            {
                InitializeComponent();
                timer = new Timer();
                timer.Interval = 1000;
                timer.Tick += Timer_Tick;
            }

            private void Timer_Tick(object sender, EventArgs e)
            {
                second++;
                if (second == 60)
                {
                    second = 0;
                    minute++;
                }
                if (minute == 60)
                {
                    minute = 0;
                    hour++;
                }
                if (hour == 24)
                {
                    hour = 0;
                }

                if (second == 0)
                {
                    Tick?.Invoke(this, EventArgs.Empty); 
                }

                if (hour == alarmHour && minute == alarmMinute && second == 0)
                {
                    Alarm?.Invoke(this, EventArgs.Empty); 
                }

                UpdateDisplay();
            }

            private void UpdateDisplay()
            {
                labelTime.Text = $"{hour:D2}:{minute:D2}:{second:D2}";
            }


            private void Form1_Load(object sender, EventArgs e)
            {
                Alarm += Form1_Alarm;
            }

            private void Form1_Alarm(object sender, EventArgs e)
            {
                MessageBox.Show("闹钟响了！");
            }

            public event EventHandler Tick;
            public event EventHandler Alarm;

            private int alarmHour;
            private int alarmMinute;
        
        private void button1_Click(object sender, EventArgs e)
        {
            hour = (int)numericUpDownHour.Value;
            minute = (int)numericUpDownMinute.Value;
            second = 0;

            UpdateDisplay();
            timer.Start();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            timer.Stop();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            hour = 0;
            minute = 0;
            second = 0;

            UpdateDisplay();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            alarmHour = (int)numericUpDownAlarmHour.Value;
            alarmMinute = (int)numericUpDownAlarmMinute.Value;
        }
    }
    }



